package com.jiangtour.controller;

import com.jiangtour.dataobject.ProductCategory;
import com.jiangtour.dataobject.SiteInfo;
import com.jiangtour.exception.SiteException;
import com.jiangtour.form.CategoryForm;
import com.jiangtour.service.CategoryService;
import com.jiangtour.service.SiteInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Title: SiteCategoryController
 * @ProjectName site
 * @Description:  站点类目
 * @date 2018/12/24/02416:03
 */
@Controller
@RequestMapping("/site/category")
public class SiteCategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SiteInfoService siteInfoService;

    /**
     * 类目列表
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(Map<String, Object> map, HttpServletRequest request) {
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(request);
        List<ProductCategory> categoryList =categoryService.findCategoryAscription(currentAccount.getId());
        map.put("categoryList", categoryList);
        return new ModelAndView("category/list", map);
    }


    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                              Map<String, Object> map) {

        if (categoryId !=null ){
            ProductCategory productCategory = categoryService.findOne(categoryId);
            map.put("category", productCategory);
        }
        return  new ModelAndView("category/index", map);
    }


    /**
     * 保存/更新
     * @param form
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid CategoryForm form,
                             BindingResult bindingResult,HttpServletRequest request,
                             Map<String, Object> map) {
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(request);
        if(bindingResult.hasErrors()){
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/site/category/index");
            return new ModelAndView("common/error", map);
        }
        ProductCategory productCategory = new ProductCategory();

        try {
            if(form.getCategoryId() !=null ){
                productCategory = categoryService.findOne(form.getCategoryId());
            }
            BeanUtils.copyProperties(form, productCategory);
            productCategory.setCategoryAscription(currentAccount.getId());
            categoryService.save(productCategory);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/site/category/index");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/site/category/list");
        return new ModelAndView("common/success", map);
    }



}
